function formatTime(time, formatStr) {
  if (!time) {
    return
  }
  var times = time.replace("-", "/").replace("-", "/")
  var date = new Date(times)
  var Y = date.getFullYear()
  var M = date.getMonth() + 1
  var D = date.getDate()
  var h = date.getHours()
  var m = date.getMinutes()
  var s = date.getSeconds()
  // 替换模板字符串
  formatStr = formatStr.replace('Y', Y > 9 ? Y : '0' + Y)
  formatStr = formatStr.replace('M', M > 9 ? M : '0' + M)
  formatStr = formatStr.replace('D', D > 9 ? D : '0' + D)
  formatStr = formatStr.replace('h', h > 9 ? h : '0' + h)
  formatStr = formatStr.replace('m', m > 9 ? m : '0' + m)
  formatStr = formatStr.replace('s', s > 9 ? s : '0' + s)
  return formatStr
}

// toast
function toast(txt) {
  wx.showToast({
    title: txt,
    icon: 'none',
    duration: 2000,
    mask: true
  })
}

// loading
function showLoading(str) {
  wx.showLoading({
    title: str,
    mask: true
  })
}

// hideLoading
function hideLoading() {
  wx.hideLoading({})
}

// 返回上一页
function toBack() {
  wx.navigateBack({
    delta: 1,
  })
}

// 上传图片
function uploadImg(opt) {
  const BASE_URL = 'https://api-hgj.hejiaic.com'
  const sign = require('./ajax/sign.js')
  const formData = {
    token: wx.getStorageSync('token') || '',
    openid: wx.getStorageSync('openid') || ''
  }
  wx.uploadFile({
    url: BASE_URL + '/wx/upload',
    filePath: opt.url,
    name: 'file',
    formData: sign.md5_sign(formData),
    success(res) {
      typeof opt.success === 'function' && opt.success(JSON.parse(res.data))
    },
    fail(err) {
      typeof opt.fail === 'function' && opt.fail(err)
    }
  })
}

module.exports = {
  formatTime,
  toast,
  showLoading,
  hideLoading,
  toBack,
  uploadImg
}
